<?php
namespace Home\Controller;
class FreepayController extends CommonController {
    public function index(){
    	$uid = $_SESSION['_loginUser_home']['uid'];
    	$this->display();
    }
    
    public function pay(){
    	$id = I('id');
    	if($id){
    		$this->info = D('YgclOrder')->where(array('id'=>$id))->relation(true)->find();
    	
    		//支付参数
    		$openid = $_SESSION['_loginUser_home']['openid'];
    		$ddbh = $this->info['ddbh'];
    		$body = $this->info['objectinfo'].',金额：'.$this->info['total'];
    		$total_fee = $this->info['total']*100;
    		$notify_url = 'http://ygcl.kongkonghou.cn/weixin/pay/notify.html';
    		$jsApiParameters = A('Weixin/Pay')->getConfig($openid,$ddbh,$body,$total_fee,$notify_url);
    		$this->jsApiParameters = $jsApiParameters;
    	}
    	$this->display();
    }
    
    
    public function getpay(){
    	$id = I('id');
    	if($id){
    		$this->info = D('YgclOrder')->where(array('id'=>$id))->relation(true)->find();
    		 
    		//支付参数
    		$openid = $_SESSION['_loginUser_home']['openid'];
    		$ddbh = $this->info['ddbh'];
    		$body = $this->info['objectinfo'].',金额：'.$this->info['total'];
    		$total_fee = $this->info['total'];
    		$notify_url = 'http://ygcl.kongkonghou.cn/weixin/pay/notify.html';
    		$jsApiParameters = A('Weixin/Pay')->getConfig($openid,$ddbh,$body,$total_fee,$notify_url);
    	}
    	$data = array(
    		'status' => 'ok',
    		'wxconf' => $jsApiParameters,
    	);
    	$this->ajaxReturn($data);
    }
    
    public function savefreepay(){
    	$m = D('YgclOrder');
    	$data = array(
    			'objectinfo' => I('info'),
    			'total' => I('total'),
    			'uid' => I('uid'),
    			'type' => 'freepay',
    			'ddbh' => date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8),
    			'create_time' => Date('Y-m-d H:i:s'),
    			'update_time' => Date('Y-m-d H:i:s'),
    			'token_time' => time()+60*30, //有效期30分钟
    	);
    	$m->create($data);
    	$id = $m->add(); 
    	if($id){
    		$this->ajaxReturn(array('status'=>1,'data'=>$id));
    	}else{
    		$this->ajaxReturn(array('status'=>0,'data'=>''));
    	}
    	
    }
    
    public function kjbm(){
    	$this->display();
    }
    
    public function savekjbm(){
    	if(IS_POST){
    		$did = I('did');
    		if(!$did){
    			$this->error('请选择校区');
    			exit();
    		}
    		$m = M('YgclOrder');
    		$data = array(
    				'did' => $did,
    				'objectinfo' => I('info'),
    				'total' => I('total'),
    				'uid' => I('uid'),
    				'type' => I('type'),
    				'ddbh' => date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8),
    				'create_time' => Date('Y-m-d H:i:s'),
    				'update_time' => Date('Y-m-d H:i:s'),
    				'token_time' => time()+60*30, //有效期30分钟
    		);
    		$m->create($data);
    		$id = $m->add();
    		if($id){
    			$this->success($id);
    		}else{
    			$this->error('提交失败');
    		}
    	}
	}
	
	public function summer(){
    	$this->display();
    }
    
    public function savesummer(){
    	if(IS_POST){
    		$did = I('did');
    		if(!$did){
    			$this->error('请选择校区');
    			exit();
			}
    		$m = M('YgclOrder');
    		$data = array(
					'did' => $did,
					'stuname' => I('stuname'),
					'tel' => I('tel'),
    				'objectinfo' => I('info'),
    				'total' => I('total'),
    				'uid' => I('uid'),
    				'type' => I('type'),
    				'ddbh' => date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8),
    				'create_time' => Date('Y-m-d H:i:s'),
    				'update_time' => Date('Y-m-d H:i:s'),
    				'token_time' => time()+60*30, //有效期30分钟
    		);
    		$m->create($data);
    		$id = $m->add();
    		if($id){
    			$this->success($id);
    		}else{
    			$this->error('提交失败');
    		}
    	}
    }
}